package com.example.test.student;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.test.R;
import com.example.test.utils.Http;
import com.example.test.utils.MyApplication;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Message extends AppCompatActivity {
    private ListView listView;
    private List<Map<String, Object>> ListViewData;

    public String[] title =  new String[3];
    public String[] content = new String[3];
    public String[] time = new String[3];
    private int isRun = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message2);
        this.init();
    }

    public void init() {
        getUrl();
    }

    public void isRun() {
        Log.d("main", "主线程开始");
        ListViewData = new ArrayList<>();
        for (int i = 0; i < title.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("title",  String.valueOf(title[i]));
            map.put("content",  String.valueOf(content[i]));
            map.put("time",  String.valueOf(time[i]));
            ListViewData.add(map);
        }
        ListView listView = findViewById(R.id.List);
        SimpleAdapter adapter = new SimpleAdapter(this, ListViewData, R.layout.message_list_item, new String[]{"title", "content","time"}, new int[]{R.id.message_item_title, R.id.message_item_content, R.id.message_item_time});
        listView.setAdapter(adapter);
        Log.d("main", "主线程结束");
    }

    public void getUrl() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                StringBuilder url = new StringBuilder();

                String res;
                res = Http.sendGet("http://36428pn632.qicp.vip/student/index.php/role/role/read");
                //                            object = new JSONObject(res);
                final LinkedHashMap<String, Object> hashMaps = Http.stringToJsonObject(res);
                Log.d("HttpGET", (String) hashMaps.get("msg"));
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d("data", "数据处理开始");
                        Map result =(Map) hashMaps.get("result");
                        Log.d("HttpGET", ""+result.size());
                        for (int i = 0; i<result.size(); i++) {
                            Map item = (Map) result.get(""+i);
                            title[i] = item.get("tltie").toString();
                            content[i] = item.get("boby").toString();
                            time[i] = item.get("create_time").toString();
                        }
                        isRun();
                        Log.d("data", "数据处理结束");
                    }
                });
            }
        }).start();
    }
}
